/*
============================================================================
 Name        : $(Basename)Client.cpp
 Author      : $(Author)
 Version     :
 Copyright   : $(Copyright)
 Description : Client for $(Basename)Server.
============================================================================
*/

#include <e32cons.h>
#include "$(Basename)ReqHandler.h"
#include "$(Basename)Client.h"

_LIT(KTextConsoleTitle, "Console");
_LIT(KTextClientServerTest, "Client-Server test\n");
_LIT(KTextFailed, " failed, leave code = %d");
_LIT(KTextPressAnyKey, " [press any key]\n");

LOCAL_D CConsoleBase* console;

LOCAL_C void MainL()
	{
	console->Write(KTextClientServerTest);

	C$(Basename)ReqHandler* async = C$(Basename)ReqHandler::NewLC();
	async->RequestTime();

	CActiveScheduler::Start();

	TTime time = async->Time();
	TBuf<KMaxShortDateFormatSpec> timebuf;
	time.FormatL(timebuf, TShortDateFormatSpec());
	console->Write(timebuf);

	CleanupStack::PopAndDestroy();
	}

LOCAL_C void DoStartL()
	{
	CActiveScheduler* scheduler = new(ELeave) CActiveScheduler();
	CleanupStack::PushL(scheduler);
	CActiveScheduler::Install(scheduler);

	MainL();

	CleanupStack::PopAndDestroy();
	}

GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
	CTrapCleanup* cleanup = CTrapCleanup::New();
	if(!cleanup)
		{
		return KErrNoMemory;
		}

	TRAPD(errno, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen, KConsFullScreen)));
	if(errno)
		{
		return errno;
		}

	// Run application code inside TRAP harness, wait keypress when terminated
	TRAP(errno, DoStartL());
	if(errno)
		{
		console->Printf(KTextFailed, errno);
		}
	console->Printf(KTextPressAnyKey);
	console->Getch();

	delete console;
	delete cleanup;
	__UHEAP_MARKEND;
	return KErrNone;
	}
